<?php
class Controller_Admin_City extends Controller_Admin_Base {
		
	protected function actionSwitch() {
		switch ($this->arrRequest[2]) {
			case '': $this->actionCitiesList(); break;
			case 'add': $this->actionAddCity(); break;
			case 'edit':
				$cityId = intval($this->arrRequest[3]);
				$objCity = new City($cityId);
				if (!$objCity->id) throw new Exception_SiteError('', 404);
				$this->actionEditCity($objCity);
				break;
			case 'article':
				switch ($this->arrRequest[3]) {
					case '': $this->actionArticleList(); break;
					case 'add': $this->actionAddArticle(); break;
					case 'edit':
						$articleId = intval($this->arrRequest[4]);
						$objArticle = new Article($articleId);
						if ($objArticle->id && $objArticle->getField('ID_essence_type') == City::getEssenceType()) $this->actionEditArticle($objArticle);
						else throw new Exception_SiteError('', 404);
						break;
					default: throw new Exception_SiteError('', 404);
				}
				break;
			default: throw new Exception_SiteError('', 404);
		}
	}
	
	/**
	 * Список всех городов
	 */
	private function actionCitiesList() {
		$objCity = new City();
		
		$arrCities = $objCity->find('all');
		
		$this->pageHead = 'Список городов (поддоменов) (<a href="/admin/city/add">Добавить</a>)';
		$this->title = 'Список городов (поддоменов)';
		
		$this->showHeader();
			View_City::showAdminCityList($arrCities);
		$this->showFooter();
	}
	
	/**
	 * Страница добавления города
	 */
	private function actionAddCity() {
		if (isset($_POST['City'])) {
			$objCity = new City($_POST['City']);
			if ($objCity->save()) User_Location::move('/admin/city');
			else print_r($objCity->arrErrors);
		}
		else $objCity = new City();
		
		$this->pageHead = $this->title = 'Добавление города (поддомена)';
		
		$this->showHeader();
			View_City::showCityForm($objCity);
		$this->showFooter();
	}
	
	/**
	 * Страница редактирования города
	 * @param City $objCity - объект города
	 */
	private function actionEditCity(City $objCity) {
		if (isset($_POST['City'])) {
			$objCity = new City($_POST['City']);
			if ($objCity->save()) User_Location::move('/admin/city');
			else print_r($objCity->arrErrors);
		}
		
		$this->pageHead = $this->title = 'Редактирование города';
		
		$this->showHeader();
			View_City::showCityForm($objCity);
		$this->showFooter();
	}
	
	public function actionArticleList() {
		$objArticle = new Article();
	
		$arrArticles = $objArticle->find('all', array(
			'conditions' => '`ID_essence_type` = ' . City::getEssenceType(),
			'order' => '`created_at` DESC'
		));
	
		$this->title = 'Список статей городов';
		$this->pageHead = 'Список статей городов (<a href="' . W_ADMIN . '/city/article/add">Добавить</a>)';
	
		$this->showHeader();
			View_Article::showAdminArticleList($arrArticles, 'city');
		$this->showFooter();
	}
	
	public function actionAddArticle() {
		if (isset($_POST['Article'])) {
			$objArticle = new Article($_POST['Article']);
			$objArticle->setField('ID_city', $objArticle->getField('ID_essence'));
			if ($objArticle->save()) User_Location::move('/admin/city/article');
			else print_r($objArticle->arrErrors);
		}
		else $objArticle = new Article();
	
		$objCity = new City();
		$arrCities = City::getCitiesForSelect();
	
		$this->title = $this->pageHead = 'Добавление статьи';
	
		$this->showHeader();
			View_Article::showArticleForm($objArticle, $objCity, $arrCities, $arrCities);
		$this->showFooter();
	}
	
	public function actionEditArticle(Article $objArticle) {
		if (isset($_POST['Article'])) {
			$objArticle = new Article($_POST['Article']);
			$objArticle->setField('ID_city', $objArticle->getField('ID_essence'));
			if ($objArticle->save()) User_Location::move('/admin/city/article');
			else print_r($objArticle->arrErrors);
		}
		
		$objCity = new City();
		$arrCities = City::getCitiesForSelect();
	
		$this->title = $this->pageHead = 'Редактирование статьи';
	
		$this->showHeader();
			View_Article::showArticleForm($objArticle, $objCity, $arrCities, $arrCities);
		$this->showFooter();
	}
	
}